﻿using System.Collections.Generic;
using Ninject;
using WPF_CAD.Model;
using System.Windows.Controls;
using WPF_CAD.Common;
using WPF_CAD.Algorithms;

namespace WPF_CAD.Strategy
{
    public abstract class StrategyBase : IStrategy
    {
        #region Atributtes
        protected int ClickCount;
        protected IKernel Kernel;
        public IFigure Figure { get; set; }

        #endregion

        #region Behaviors

        public abstract IFigure PerformMouseLeftClicDownEvent(Canvas canvas, IPoint point, IEnumerable<IFigure> figures);


        public abstract IFigure PerformMouseRightClicDownEvent(Canvas canvas, IPoint point, IEnumerable<IFigure> figures);


        #endregion

        #region Constructor
        protected StrategyBase(IFigure figure)
        {
            Figure = figure;
            Kernel = new StandardKernel(new CadModule());
        }
        #endregion

        #region Methods
        protected void DrawFigureInCanvas(Canvas canvas, IPoint point)
        {
            GetLineAndAddPointsToCollection(point);

            FigureDrawer.DrawFigure(canvas, Figure);
        }

        protected IFigure CloseFigureDrawing()
        {
            Figure.DrawStatus = DrawStatus.Completed;
            ClickCount = 0;

            return Figure;
        }

        protected void ValidateFigureIsNew<T>() where T : IFigure
        {
            if (Figure.PointsCollection.Count != 0) Figure = Kernel.Get<T>();
        }

        private void GetLineAndAddPointsToCollection(IPoint point)
        {
            var lineRepresentation = BressenhamLine.GetLineRepresentation(Figure.FinalPoint, point);

            foreach (var pointInLine in lineRepresentation)
                Figure.PointsCollection.Add(pointInLine);
        }

        #endregion

    }
}
